#include "logo.h"

double evaluation(char string[])
{
  float answer[100] ={0};
  double calculation[100]={0};
  int operate[100] ={0};
  char number[100] = "\0";
  int i;
  int j = 0;
  double a;
  double b;
  int opLen = 0;
  int ansLen = 0;
  int cacLen = 0;
  int lenS = strlen(string);
  int lenN = 0;
  
  number[0] = '\0';
//  printf("string is %s\n",string);
  for (i = 0;i < lenS;i++)
  {
    while (isspace(string[i])) 
    {
      continue;      
    }
    while (((isdigit(string[i])) || (string[i] == '.')) && (i < lenS) )
	{
//      printf("string[%d] is %c\n",i,string[i]);
	  number[lenN++] = string[i];
	  number[lenN] = '\0';
      printf("number is %s\n",number);
      i++;
	}
	if (number[0] != '\0' )
	{
      answer[ansLen++] =  atof(number);
      printf("number is %s\n",number);
      printf("answer[%d] is %f\n", ansLen-1,answer[ansLen-1]);
      number[0] = '\0';
	  lenN = 0;
    }
    while (isspace(string[i])) 
    {
      continue;      
    }
    if (i >= lenS)
    {
      break;
    }
	switch (string[i]){ 
	case '+' :for (j = opLen; (j > 0) && (!(operate[j-1] == -5)); j--)
	            {
				  answer[ansLen++] = operate[j-1];
				  operate[--opLen] = 0;
				}
			  operate[opLen++] = -1;
			  break;
	case '-' :for (j = opLen; (j > 0) && (!(operate[j-1] == -5)); j--)
	            {
				  answer[ansLen++] = operate[j];
				  operate[--opLen] = 0;
				}
			  operate[opLen++] = -2;
			  break;
	case '*' :for (j = opLen; (j > 0) && (!(operate[j-1] == -5)) && (!(operate[j-1] == -1)) && (!(operate[j-1] == -2)); j--)
	            {
				  answer[ansLen++] = operate[j-1];
				  operate[--opLen] = 0;
				}
			  operate[opLen++] = -3;
			  break;
	case '/' :for (j = opLen; (j > 0) && (!(operate[j-1] == -5)) && (!(operate[j-1] == -1)) && (!(operate[j-1] == -2)); j--)
	            {
				  answer[ansLen++] = operate[j-1];
				  operate[--opLen] = 0;
				}
			  operate[opLen++] = -4;
			  break;
	case '(' :operate[opLen++] = -5;
			  break;
	case ')':for (j = opLen; (j > 0) && (!(operate[j-1] == -5)); j--)
	            {
				  answer[ansLen++] = operate[j-1];
				  operate[--opLen] = 0;
				}
			  break;
	default  :goto Error;}
  }
  
  for (i= 0; i< opLen; i++)
  {
      printf("the operate[%d] is %d\n", i, operate[i]);
  }
  
  while (opLen)
	{ 
      if (operate[opLen-1] ==-5)
      {
         opLen--;
         operate[opLen] = 0;
         continue;
      }
      opLen--;
      answer[ansLen++] = operate[opLen];
      printf("answer[%d] is %f\n", ansLen-1,answer[ansLen-1]);
	  operate[opLen] = 0;
    }

  for (i = 0;i< ansLen; i++)
  {
    printf("answer[%d] is %f\n", i,answer[i]);
    if (answer[i] >= 0)
	{
	  calculation[cacLen++] = answer[i];
	  printf("calculation[%d] is %f\n", cacLen-1,calculation[cacLen-1]);
	}
	else 
	{
      printf("answer[%d] is %d\n", i,(int)answer[i]);
	  switch ((int)answer[i]) {
	  case -1 :
	          if (cacLen < 1) 
			  {
			    goto Error;
			  }
			  a = calculation[--cacLen];
			  b = calculation[--cacLen];
			  calculation[cacLen++] = b + a;
			  printf("calculation[%d] is %f\n", cacLen-1,calculation[cacLen-1]);
              break;			  
	  case -2 :if (cacLen < 1) 
			  {
			    goto Error;
			  }
			  a = calculation[--cacLen];
			  b = calculation[--cacLen];
			  calculation[cacLen++] = b - a;
			  break;
	  case -3 :if (cacLen < 1) 
			  {
			    goto Error;
			  }
			  a = calculation[--cacLen];
			  b = calculation[--cacLen];
			  calculation[cacLen++] = b * a;
			  break;
	  case -4 :if (cacLen < 1) 
			  {
			    goto Error;
			  }
			  a = calculation[--cacLen];
			  b = calculation[--cacLen];
			  calculation[cacLen++] = b / a;
              break;}
	}
  }
  if (cacLen-1)
  {
    goto Error;
  }
  printf("calculation is %f\n",calculation[0]);
  return calculation[0];
  
  Error:
        return 0;
}
